package zouhaotian.rbac.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 异常处理工具类
 *
 * @auther 邹昊天
 * @since 2025/6/21 22:03
 */
public class ExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);

    private ExceptionHandler(){

    }

    private static void printError(String message) {

        System.out.println("\033[31m" + message + "\033[0m");
    }

    public static void handle(Exception e){
        if (e instanceof FatalException){
            logger.error("致命错误：{}", e.getMessage(), e);
            printError(e.getMessage());
            System.exit(1);
            return;
        }

        if (e instanceof  PersistenceException) {
            logger.error("数据持久化异常：{}", e.getMessage(), e);
            printError(e.getMessage());
            return;
        }

        if (e instanceof  BaseRuntimeException) {
            logger.error("运行异常：{}", e.getMessage(), e);
            printError(e.getMessage());
            return;
        }
        logger.error("未知异常：{}", e.getMessage(), e);
        printError(e.getMessage());
    }
}
